package com.lmb.todo.service.impl;

import com.lmb.todo.dao.FolderDao;
import com.lmb.todo.pojo.Folder;
import com.lmb.todo.service.FolderService;
import com.lmb.todo.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lmb
 */
@Service
public class FolderServiceImpl implements FolderService {

    @Autowired
    private FolderDao folderDao;

    @Override
    public List<Folder> getAllByUserId(Integer userId) {
        if (userId < 0) {
            return null;
        }
        return folderDao.getAllByUserId(userId);
    }

    @Override
    public Integer add(Folder folder){
        if (folder != null) {
            folder.setCreateTime(DateUtil.getCurrentDateStr());
            folder.setUpdateTime(null);
            return folderDao.add(folder);
        }
        return null;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer deleteAll(List<Integer> ids){
        if (ids != null) {
            for (Integer id : ids) {
                updateGroupByFolderId(id, null);
            }
            return folderDao.deleteAll(ids);
        }
        return null;
    }

    @Override
    public Integer updateGroupByFolderId(Integer folderId, Integer updateId) {
        if (folderId > 0) {
            folderDao.updateGroupByFolderId(folderId, updateId);
        }
        return null;
    }
}
